﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ksp.Controller
{
    public static class PartParser
    {
        internal static String KeyWord = "PART";
        private static String Part = "part = ";
        private static String PartName = "partName = ";
        private static String Position = "pos = ";
        private static String Rotation = "rot = ";
        private static String AttRotation = "attRot = ";
        private static String Mir = "mir = ";
        private static String IstG = "istg = ";
        private static String DstG = "dstg = ";
        private static String Sidx = "sidx = ";
        private static String Sqor = "sqor = ";
        private static String Attm = "attm = ";

        public static Model.Part ParsePart(List<String> lines)
        {
            Model.Part part = new Model.Part();
            int lineNo = 0;
            while (lineNo < lines.Count-1)
            {
                #region Parsing
                if (lines[lineNo].StartsWith(Part))
                    part.Title = lines[lineNo].Replace(Part, null);
                if (lines[lineNo].StartsWith(PartName))
                    part.PartName = lines[lineNo].Replace(PartName, null);
                if (lines[lineNo].StartsWith(Position))
                    part.Pos = lines[lineNo].Replace(Position, null);
                if (lines[lineNo].StartsWith(Rotation))
                    part.Rot = lines[lineNo].Replace(Rotation, null);
                if (lines[lineNo].StartsWith(AttRotation))
                    part.AttRot = lines[lineNo].Replace(AttRotation, null);
                if (lines[lineNo].StartsWith(Mir))
                    part.Mir = lines[lineNo].Replace(Mir, null);
                if (lines[lineNo].StartsWith(IstG))
                    part.Istg = lines[lineNo].Replace(IstG, null);
                if (lines[lineNo].StartsWith(DstG))
                    part.Dstg = lines[lineNo].Replace(DstG, null);
                if (lines[lineNo].StartsWith(Sidx))
                    part.Sidx = lines[lineNo].Replace(Sidx, null);
                if (lines[lineNo].StartsWith(Sqor))
                    part.Sqor = lines[lineNo].Replace(Sqor, null);
                if (lines[lineNo].StartsWith(Attm))
                    part.Attm = lines[lineNo].Replace(Attm, null);
                
                // Parse Modules
                if (lines[lineNo].StartsWith(ModuleParser.KeyWord))
                {
                    List<String> moduleLines = SubSectionParser.ParseSubSection(lines, ref lineNo);
                    Model.Module module = ModuleParser.ParseModule(moduleLines);
                    part.Modules.Add(module);
                }

                // Parse Events
                if (lines[lineNo] == EventParser.KeyWord)
                {
                    List<String> eventLines = SubSectionParser.ParseSubSection(lines, ref lineNo);
                    List<Model.Event> events = EventParser.ParseEvent(eventLines);
                    part.Events.AddRange(events);
                }

                // Parse Actions
                if (lines[lineNo] == ActionParser.KeyWord)
                {
                    List<String> actionLines = SubSectionParser.ParseSubSection(lines, ref lineNo);
                    List<Model.Action> actions = ActionParser.ParseAction(actionLines);
                    part.Actions.AddRange(actions);
                }
                #endregion
                lineNo++;
            }
            return part;
        }
    }
}
